package biz.support;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.model.BbsUser;

public class AdminFilter implements Filter
{
	
	public void init(FilterConfig config) throws ServletException
	{
		
	}
	
	public void destroy()
	{

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		BbsUser adminUser = (BbsUser)request.getSession().getAttribute("user");
		if(null == adminUser){
			response.sendRedirect("../commonAction.do?method=adminLoginPre");
		}else{
			String adminUsername = adminUser.getUsername();
			int adminUsergrade = adminUser.getUsergrade();
	
			if(null != adminUsername && !"".equals(adminUsername) && (0 == adminUsergrade))
			{
				chain.doFilter(request, response);
			}
			else
			{
				response.sendRedirect("../commonAction.do?method=adminLoginPre");
			}
		}
	}
}
